package util.bd.hsql.conexion;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.BD.hsql.modelo.BaseDeDatos;

/**
 *
 * @author Administrador
 */
public class Conexion {

    public static Connection con = null;
    public static Statement sentencia = null;
    public static ResultSet rs = null;
    private static String parametros;

    public static void conexion(String server, String db, String uid, String pwd) {
        try {
            parametros = "jdbc:odbc:Driver={SQL Server};"
                    + "server=" + server + ";"
                    + "DataBase=" + db + ";"
                    + "Uid=" + uid + ";"
                    + "Pwd=" + pwd + ";";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection(parametros);
        } catch (SQLException e1) {
            javax.swing.JOptionPane.showMessageDialog(null, "Error en la Conexion al Motor SQL", "Error Conexion", 0);
            System.exit(0);
        } catch (Exception e2) {
            javax.swing.JOptionPane.showMessageDialog(null, "Error en la Conexion al Motor", "Error Conexion", 0);
            System.exit(0);
        }
    }

    public static void conexionHSQL(String nombreBD) {
        try {
            parametros = "jdbc:hsqldb:file:" + nombreBD;
            try {
                Class.forName("org.hsqldb.jdbcDriver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
            }
            con = DriverManager.getConnection(parametros);
            crearBD(BaseDeDatos.getScriptBaseDeDatos());
        } catch (SQLException e1) {
        }
    }

    private static void crearBD(String query) {
        try {
            sentencia = con.createStatement();
            sentencia.executeUpdate(query);
            sentencia.close();
        } catch (SQLException ex) {
        }
    }

    public static void guardarCambias() {
        try {
            sentencia = con.createStatement();
            sentencia.executeUpdate("SHUTDOWN");
            sentencia.close();
            con.close();
            System.out.println("ok con");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
